#include <iostream>
#include <iterator>
using namespace std;


class Data1 {
public:
    Data1(int year = 0, int month = 1, int day = 0)
        :_year(year)
        , _month(month)
        , _day(day)
    {}
    int getmounthday(int year, int month)
    {
        int num[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        {
            num[2] = 29;

        }
        return num[month];
    }
    void getdayformdata(int year, int day)
    {
        int i = 1;
        int cutday = day;

        while (cutday)
        {
            int num[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            {
                num[2] = 29;
            }
            if (cutday > num[i])
            {
                cutday -= num[i];
                i++;
                _month++;
            }
            else {
                break;
            }
            if (_month > 12)
            {
                _year++;
                _month = 1;
            }

        }
        _day = cutday;
    }
    void print()
    {
        cout << _year << "-";
        if (_month < 10)
        {
            cout << "0" << _month;
        }
        else {
            cout << _month;

        }
        if (_day < 10)
        {
            cout << "-" << "0" << _day;
        }
        else {
            cout << "-" << _day;

        }
        cout << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
int main1() {
    int year;
    int day;
    cin >> year;
    Data1 test(year);
    cin >> day;
    test.getdayformdata(year, day);
    test.print();
    //getmonthday(year);
}


class Data
{
public:
    Data(int year = 0, int month = 0, int day = 0)
        :_year(year)
        , _month(month)
        , _day(day)
    {}
    void cinn()
    {
        cin >> _year;
        cin >> _month;
        cin >> _day;
    }
    void addcountdaya(int countday)
    {
        _day += countday;
        int num1[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        while (_day > num1[_month])
        {
            int num[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
            if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0)
            {
                num[2] = 29;
            }
            if (_day > num[_month])
            {
                _day -= num[_month];
                _month++;
            }
            if (_month > 12)
            {
                _year++;
                _month = 1;
            }
        }
    }
    void print()
    {
        cout << _year << "-";
        if (_month < 10)
        {
            cout << "0" << _month;
        }
        else {
            cout << _month;
        }
        if (_day < 10)
        {
            cout << "-" << "0" << _day;
        }
        else {
            cout << "-" << _day;

        }
        cout << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
int main() {
    int count;
    cin >> count;
    int countday;
    int i = 0;
    Data* cut = new Data[count];
    while (i < count)
    {
        cut[i].cinn();
        cin >> countday;
        cut[i].addcountdaya(countday);
        cut[i].print();
        ++i;
    }
    delete[] cut;
}